home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 February / EnigmA AMIGA RUN 15 (1997)(G.R. Edizioni)(IT)[!][issue 1997-02][PLANET CD V].iso / enigma / earcd / sviluppo / svilupp2 / gmsppr10.lha / Joystick.c < prev    next >
C/C++ Source or Header  |  1996-09-01  |  6KB  |  193 lines

  1. #ifndef DEVICES_GAMEPORT_H
  2. #include <devices/gameport.h>
  3. #endif
  4.  
  5. #include <proto/exec.h>
  6.  
  7. #include "Global.h"
  8.  
  9. /************************************************************************/
  10.  
  11. struct SignalSemaphore JoystickSemaphore;
  12.  
  13. /****** gamesupport.library/GS_AllocateJoystick **************************
  14. *
  15. *   NAME
  16. *    GS_AllocateJoystick -- allocate the joystick
  17. *
  18. *   SYNOPSIS
  19. *    success = GS_AllocateJoystick(ReplyPort,ControllerType)
  20. *       d0                            a0           d0
  21. *
  22. *    ULONG GS_AllocateJoystick(struct MsgPort *, UBYTE);
  23. *
  24. *   FUNCTION
  25. *    Allocate gameport 1 by assigning it the specified controller
  26. *    type.
  27. *    If you allocate GPCT_ABSJOYSTICK, the trigger will be set
  28. *    to report all joystick events and no timeouts.
  29. *    You can use GameSupportBase->Joystick.Request and
  30. *    GameSupportBase->Joystick.Event to access the controller.
  31. *
  32. *   INPUTS
  33. *    ReplyPort      - the port to use for the device I/O
  34. *    ControllerType - the controller type (see <devices/gameport.h>)
  35. *
  36. *   RESULT
  37. *    success - TRUE if you got the joystick. FALSE if someone else
  38. *        still owns it. You might want to try again later.
  39. *
  40. *   SEE ALSO
  41. *    GS_FreeJoystick(), GS_SendJoystick(), <devices/gameport.h>
  42. *
  43. *************************************************************************/
  44.  
  45. SAVEDS_ASM_D0A0(ULONG,LibGS_AllocateJoystick,UBYTE,ControllerType,struct MsgPort *,ReplyPort)
  46.  
  47. {
  48.   if (AttemptSemaphore(&JoystickSemaphore))
  49.     {
  50.       if (GameSupportBase->Joystick.Request.io_Device==NULL)
  51.     {
  52.       GameSupportBase->Joystick.Request.io_Message.mn_ReplyPort=ReplyPort;
  53.       if (OpenDevice("gameport.device",1L,&GameSupportBase->Joystick.Request,0L)==0)
  54.         {
  55.           BYTE TheControllerType;
  56.  
  57.           GameSupportBase->Joystick.Request.io_Command=GPD_ASKCTYPE;
  58.           GameSupportBase->Joystick.Request.io_Length=1;
  59.           GameSupportBase->Joystick.Request.io_Data=&TheControllerType;
  60.           TheControllerType=GPCT_NOCONTROLLER;
  61.           Forbid();
  62.           DoIO(&GameSupportBase->Joystick.Request);
  63.           if (TheControllerType==GPCT_NOCONTROLLER)
  64.         {
  65.           GameSupportBase->Joystick.Request.io_Command=GPD_SETCTYPE;
  66.           GameSupportBase->Joystick.Request.io_Length=1;
  67.           GameSupportBase->Joystick.Request.io_Data=&TheControllerType;
  68.           TheControllerType=ControllerType;
  69.           DoIO(&GameSupportBase->Joystick.Request);
  70.           Permit();
  71.           if (ControllerType==GPCT_ABSJOYSTICK)
  72.             {
  73.               static struct GamePortTrigger JoystickTrigger=
  74.             {
  75.               GPTF_UPKEYS | GPTF_DOWNKEYS,
  76.               0,
  77.               1, 1
  78.             };
  79.  
  80.               GameSupportBase->Joystick.Request.io_Command=GPD_SETTRIGGER;
  81.               GameSupportBase->Joystick.Request.io_Data=&JoystickTrigger;
  82.               GameSupportBase->Joystick.Request.io_Length=sizeof(JoystickTrigger);
  83.               DoIO(&GameSupportBase->Joystick.Request);
  84.             }
  85.           GameSupportBase->Joystick.Request.io_Command=CMD_CLEAR;
  86.           DoIO(&GameSupportBase->Joystick.Request);
  87.           return TRUE;
  88.         }
  89.           Permit();
  90.           CloseDevice(&GameSupportBase->Joystick.Request);
  91.         }
  92.       GameSupportBase->Joystick.Request.io_Device=NULL;
  93.     }
  94.       ReleaseSemaphore(&JoystickSemaphore);
  95.     }
  96.   return FALSE;
  97. }
  98.  
  99. /****** gamesupport.library/GS_SendJoystick ******************************
  100. *
  101. *    NAME
  102. *    GS_SendJoystick -- send joystick request
  103. *
  104. *    SYNOPSIS
  105. *    GS_SendJoystick()
  106. *
  107. *    void GS_SendJoystick(void);
  108. *
  109. *    FUNCTION
  110. *    Initialize the GameSupportBase->Joystick.Request request
  111. *    as GPD_READEVENT and send it to the gameport device.
  112. *    Basically, joystick handling works like this:
  113. *
  114. *    GS_AllocateJoystick()
  115. *    GS_SendJoystick()
  116. *    while (..)
  117. *      {
  118. *        Wait until GameSupportBase->Joystick.Request arrives at
  119. *          your ReplyPort
  120. *        Process GameSupportBase->Joystick.Event
  121. *        GS_SendJoystick()
  122. *      }
  123. *    AbortIO()
  124. *    GS_FreeJoystick()
  125. *
  126. *************************************************************************/
  127.  
  128. SAVEDS(void,LibGS_SendJoystick)
  129.  
  130. {
  131.   GameSupportBase->Joystick.Request.io_Command=GPD_READEVENT;
  132.   GameSupportBase->Joystick.Request.io_Length=sizeof(GameSupportBase->Joystick.Event);
  133.   GameSupportBase->Joystick.Request.io_Data=&GameSupportBase->Joystick.Event;
  134.   SendIO(&GameSupportBase->Joystick.Request);
  135. }
  136.  
  137. /****** gamesupport.library/GS_FreeJoystick ******************************
  138. *
  139. *   NAME
  140. *    GS_FreeJoystick -- free the joystick
  141. *
  142. *   SYNOPSIS
  143. *    GS_FreeJoystick()
  144. *
  145. *    void GS_FreeJoystick(void);
  146. *
  147. *   FUNCTION
  148. *    Free the joystick you have allocated with GS_AllocateJoystick().
  149. *    This makes the joystick available for other programs to use.
  150. *
  151. *   NOTE
  152. *    You should only hold the joystick while your window is
  153. *    active. Remember, this is a non shareable resource!
  154. *    So, it seems reasonable to to adopt the idea of a window
  155. *    having the focus to the joystick as well: as long as a
  156. *    window has the focus, input is directed to that window.
  157. *
  158. *    NOTE
  159. *    The joystick is allocated on a per-task basis. The same task
  160. *    that successfully called GS_AllocateJoystick must call
  161. *    GS_FreeJoystick().
  162. *
  163. *    NOTE
  164. *    You may call this function even if you don't own the
  165. *    joystick. In this case, nothing happens.
  166. *
  167. *    SEE ALSO
  168. *    GS_AllocateJoystick()
  169. *
  170. *************************************************************************/
  171.  
  172. SAVEDS(void,LibGS_FreeJoystick)
  173.  
  174. {
  175.   if (AttemptSemaphore(&JoystickSemaphore))
  176.     {
  177.       if (GameSupportBase->Joystick.Request.io_Device!=NULL)
  178.     {
  179.       BYTE ControllerType;
  180.  
  181.       GameSupportBase->Joystick.Request.io_Command=GPD_SETCTYPE;
  182.       GameSupportBase->Joystick.Request.io_Length=1;
  183.       GameSupportBase->Joystick.Request.io_Data=&ControllerType;
  184.       ControllerType=GPCT_NOCONTROLLER;
  185.       DoIO(&GameSupportBase->Joystick.Request);
  186.       CloseDevice(&GameSupportBase->Joystick.Request);
  187.       GameSupportBase->Joystick.Request.io_Device=NULL;
  188.       ReleaseSemaphore(&JoystickSemaphore);
  189.     }
  190.       ReleaseSemaphore(&JoystickSemaphore);
  191.     }
  192. }
  193.